package com.sjgs.gis.service;

import com.sjgs.gis.service.dto.enumeration.PluginType;
import com.sjgs.gis.service.dto.PluginDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 插件管理
 * @author jxw
 */
@Service
public class PluginDeployService implements DeployService {

    private final Logger log = LoggerFactory.getLogger(PluginDeployService.class);

    @Value("${deploy.download-dir:/tmp}")
    private String downloadDir;

    @Value("${deploy.app-dir:/usr/local/tomcat/webapps}")
    private String appDir;

    @Value("${deploy.data-dir:/root/data}")
    private String dataDir;

    @Value("${deploy.native-dir:/usr/local/tomcat/native-jni-lib}")
    private String nativeDir;

    @Value("${deploy.plugin-dir:/WEB-INF/lib}")
    private String pluginDir;


    private final  FileService fileService;

    public PluginDeployService(FileService fileService) {
        this.fileService = fileService;
    }

    public boolean deployPlugin(PluginDTO pluginDTO) {
        Path pluginPath = fileService.downloadFile(pluginDTO.getFkey());
        if (pluginPath != null) {
            try {
                List<Path> files = Files.list(pluginPath).collect(Collectors.toList());
                if (pluginDTO.getType().equals(PluginType.NATIVE)) {
                    //copy all so file to nativa-lib

                }
                //copy jar to plugin-dir
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        return false;
    }
}
